'use strict';
var baseUrl = 'http://localhost:8080/rest/tea/';
var appConfig = {
  useAuthTokenHeader : true
};
angular
  .module('sbAdminApp', [
    'oc.lazyLoad',
    'ui.router',
    'ui.bootstrap',
    'angular-loading-bar',
    'sbAdminAppServices',
    'ngCookies'
  ])
  .config(['$stateProvider', '$urlRouterProvider', '$httpProvider', '$ocLazyLoadProvider', '$locationProvider', '$cookieStoreProvider',
    function($stateProvider, $urlRouterProvider, $httpProvider, $ocLazyLoadProvider, $locationProvider, $cookieStoreProvider) {

      $ocLazyLoadProvider.config({
        debug: false,
        events: true,
      });

      $urlRouterProvider.otherwise('/dashboard/home');

      $stateProvider
        .state('dashboard', {
          url: '/dashboard',
          templateUrl: 'views/dashboard/main.html',
          resolve: {
            loadMyDirectives: function($ocLazyLoad) {
              return $ocLazyLoad.load({
                name: 'sbAdminApp',
                files: [
                  'scripts/controllers/groupControllers.js',
                  'scripts/directives/header/header.js',
                  'scripts/directives/header/header-notification/header-notification.js',
                  'scripts/directives/sidebar/sidebar.js',
                  'scripts/directives/sidebar/sidebar-search/sidebar-search.js'
                ]
              }),
              $ocLazyLoad.load({
                name: 'toggle-switch',
                files: ["bower_components/angular-toggle-switch/angular-toggle-switch.min.js",
                  "bower_components/angular-toggle-switch/angular-toggle-switch.css"
                ]
              }),
              $ocLazyLoad.load({
                name: 'ngAnimate',
                files: ['bower_components/angular-animate/angular-animate.js']
              })
              $ocLazyLoad.load({
                name: 'ngCookies',
                files: ['bower_components/angular-cookies/angular-cookies.js']
              })
              $ocLazyLoad.load({
                name: 'ngResource',
                files: ['bower_components/angular-resource/angular-resource.js']
              })
              $ocLazyLoad.load({
                name: 'ngSanitize',
                files: ['bower_components/angular-sanitize/angular-sanitize.js']
              })
              $ocLazyLoad.load({
                name: 'ngTouch',
                files: ['bower_components/angular-touch/angular-touch.js']
              })
            }
          }
        })
        .state('dashboard.home', {
          url: '/home',
          controller: 'MainCtrl',
          templateUrl: 'views/dashboard/home.html',
          resolve: {
            loadMyFiles: function($ocLazyLoad) {
              return $ocLazyLoad.load({
                name: 'sbAdminApp',
                files: [
                  'scripts/controllers/main.js'
                ]
              })
            }
          }
        })
        .state('dashboard.form', {
          templateUrl: 'views/form.html',
          url: '/form'
        })
        .state('dashboard.course', {
          templateUrl: 'views/dashboard/course.html',
          url: '/course/{couId}',
          controller: 'CourseCtrl',
          resolve: {
            loadMyFiles: function($ocLazyLoad) {
              return $ocLazyLoad.load({
                name: 'sbAdminApp',
                files: [
                  'scripts/directives/dashboard/stats/stats.js'
                ]
              })
            }
          }
        })
        .state('dashboard.course.info', {
          templateUrl: 'views/course/courseinfo.html',
          url: '/info',
          controller: 'CourseInfoCtrl'
        })
        .state('dashboard.course.addexam', {
          templateUrl: 'views/course/addexam.html',
          url: '/addexam',
        })
        .state('dashboard.course.students', {
          templateUrl: 'views/course/coursestudents.html',
          url: '/students',
          controller: 'CourseStudentsCtrl'
        })
        .state('dashboard.course.exams', {
          templateUrl: 'views/course/courseexams.html',
          url: '/exams'
        })
        .state('dashboard.course.locate', {
          templateUrl: 'views/course/courselocate.html',
          url: '/locate',
          controller: 'CourseLocateCtrl'
        })
        .state('dashboard.course.public', {
          templateUrl: 'views/course/coursepublic.html',
          url: '/public',
          controller: 'CoursePublicCtrl'
        })
        .state('dashboard.course.examdetail', {
          templateUrl: 'views/course/examdetail.html',
          url: '/examdetail'
        })
        .state('dashboard.course.publicadd', {
          templateUrl: 'views/course/coursenoticeadd.html',
          url: '/publicadd',
          controller: 'CoursePublicAddCtrl'
        })
        .state('dashboard.course.edit', {

          url: '/edit',
          views: {
            '@dashboard': {
              templateUrl: 'views/dashboard/editcourse.html',
              controller: 'EditcourseCtrl'
            }
          },
          resolve: {
            loadMyFiles: function($ocLazyLoad) {
              return $ocLazyLoad.load({
                name: 'sbAdminApp',
                files: [
                  'scripts/controllers/groupControllers.js'
                ]
              })
            }
          }
        })
        .state('dashboard.blank', {
          templateUrl: 'views/pages/blank.html',
          url: '/blank'
        })
        .state('login', {
          url: '/login',
          controller: 'LoginCtrl',
          templateUrl: 'views/pages/login.html',
          resolve: {
            loadMyFiles: function($ocLazyLoad) {
              return $ocLazyLoad.load({
                name: 'sbAdminApp',
                files: [
                  'scripts/services/services.js',
                  'scripts/controllers/loginCtrl.js'
                ]
              })
            }
          }
        })
        .state('dashboard.addcourse', {
          url: '/addcourse',
          controller: 'AddcourseCtrl',
          templateUrl: 'views/dashboard/addcourse.html',
          resolve: {
            loadMyFiles: function($ocLazyLoad) {
              return $ocLazyLoad.load({
                name: 'sbAdminApp',
                files: [
                  'scripts/controllers/groupControllers.js'
                ]
              })
            }
          }
        })
        .state('dashboard.chart', {
          templateUrl: 'views/chart.html',
          url: '/chart',
          controller: 'ChartCtrl',
          resolve: {
            loadMyFile: function($ocLazyLoad) {
              return $ocLazyLoad.load({
                name: 'chart.js',
                files: [
                  'bower_components/angular-chart.js/dist/angular-chart.min.js',
                  'bower_components/angular-chart.js/dist/angular-chart.css'
                ]
              }),
              $ocLazyLoad.load({
                name: 'sbAdminApp',
                files: ['scripts/controllers/chartContoller.js']
              })
            }
          }
        })
        .state('dashboard.table', {
          templateUrl: 'views/table.html',
          url: '/table'
        })
        .state('dashboard.panels-wells', {
          templateUrl: 'views/ui-elements/panels-wells.html',
          url: '/panels-wells'
        })
        .state('dashboard.buttons', {
          templateUrl: 'views/ui-elements/buttons.html',
          url: '/buttons'
        })
        .state('dashboard.notifications', {
          templateUrl: 'views/ui-elements/notifications.html',
          url: '/notifications'
        })
        .state('dashboard.typography', {
          templateUrl: 'views/ui-elements/typography.html',
          url: '/typography'
        })
        .state('dashboard.icons', {
          templateUrl: 'views/ui-elements/icons.html',
          url: '/icons'
        })
        .state('dashboard.grid', {
          templateUrl: 'views/ui-elements/grid.html',
          url: '/grid'
        });

      /* Register error provider that shows message on failed requests or redirects to login page on
       * unauthenticated requests */
      $httpProvider.interceptors.push(function($q, $rootScope, $location) {
        return {
          'responseError': function(rejection) {
            var status = rejection.status;
            var config = rejection.config;
            var method = config.method;
            var url = config.url;

            if (status == 401) {
              $location.path("/login");
            } else {
              $rootScope.error = method + " on " + url + " failed with status " + status;
            }

            return $q.reject(rejection);
          }
        };
      });

      /* Registers auth token interceptor, auth token is either passed by header or by query parameter
       * as soon as there is an authenticated user */
      $httpProvider.interceptors.push(function($q, $rootScope, $location, $cookieStore) {
        return {
          'request': function(config) {
            var isRestCall = true;
            // console.log($rootScope.authToken);
            $rootScope.authToken = $cookieStore.get('authToken');
            if (isRestCall && angular.isDefined($rootScope.authToken)) {
              var authToken = $rootScope.authToken;

              if (appConfig.useAuthTokenHeader) {
                config.headers['X-Auth-Token'] = authToken;
              } else {
                config.url = config.url + "?token=" + authToken;
              }
            }
            return config || $q.when(config);
          }
        }
      });
    }
  ])
 